/**
 * 右侧导航
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var ui = require("ui");
    require("permission");
    require('global');
    var api_prefix = G_CONFIG["api_prefix"] + "tools/basis-function-plugin/"; //api前缀
    var token = util.getToken()

    var service = {
        checkNumber: function(){
            var reg = /^(0|[1-9]\d*)(\.\d+)?$/;
            var input_num = $(".js_number");
            input_num.each(function(i,item){
                // 过滤输入的错误数据
                $(item).on("blur",function(){
                    var new_value = $(this).val();
                    if($.trim(new_value)===''){
                        return
                    }
                    // 匹配格式是否正确
                    if(!reg.test(new_value)){
                        // 去掉不合法的字符
                        new_value = new_value.replace(/[^.\d]/g,"");
                        if(new_value){
                            new_value = new_value.match(/^\d+(\.\d+)?/)[0];
                        }
                        // 去掉第一个小数点之后多的小数点和数字
                        $(this).val(new_value);
                    }
                });

            });
        },

        initSelect: function () {
            // 下拉框选择
            $(".js-select").on("click",function(e){
                $(".select").removeClass("select-on"); 
                //var text = $(this).find(".select-text");
                $(this).parent().addClass("select-on");
                e.stopPropagation();
            });

            //点击桌面隐藏弹层
            $(document).click(function(e){
                $(".select").removeClass("select-on"); 
            });
        },

        checkForm: function () {
            var input_num = $(".js_number");
            input_num.removeClass("input-error");
            var beal = true;
            input_num.each(function(){
                if($(this).val()===""){
                    util.alert("数据不能为空");
                    $(this).addClass("input-error");
                    beal = false;
                    return false;
                }
            });
            if ($('#minArea').val() != $('#minArea2').val()) {
               util.alert('最小平方要一样');
               $('#minArea, #minArea2').addClass("input-error");
               return false;
            }
            else if ($('#maxArea2').val() != $('#maxArea3').val()) {
               util.alert('最大平方要一样');
               $('#maxArea2, #maxArea3').addClass("input-error");
               return false;
            }
            else if (parseInt($('#minArea2').val()) >= parseInt($('#maxArea2').val())) {
               util.alert('最小平方不能大于最大平方');
               $('#minArea2').addClass("input-error");
               return false;
            }
            else if ($("#selectNoMode").val() == "房款总额" && parseInt($('#txtNoElevatorNum').val()) > 100) {
               util.alert('不能大于100%');
               return false;
            }
            else if ($("#selectHaveMode").val() == "房款总额" && parseInt($('#txtHaveElevatorNum').val()) > 100) {
               util.alert('不能大于100%');
               return false;
            }
            return beal;
        },

        getValue: function () {
            var url = api_prefix + "get-tax-setting";
            util.myAjax({
                url: url,
                type: "GET",
                cache: false,
                context: this,
                success: function(json) {
                    if (json.retCode == 0) {
                        var data = json.data || {}; 
                        this.setValue(data);
                    } else {
                        ui.showErrTip(json.errMsg);
                    }
                },
                complete:function(){
                    ui.removeLoading();
                }
            });
        },

        setValue: function (data) {
            if(!data.yinhua){
                return
            }
            var tyoumode = data.tyoumode + "1";
            var twumode = data.twumode + "1";
            if (tyoumode != 1) {
                tyoumode = tyoumode.substring(0, tyoumode.length - 1);
                if (tyoumode == 1) {
                    $('#selectHaveUnit').val("元");
                    $('#selectHaveMode').val("面积");
                } else if (tyoumode == 2) {
                    $('#selectHaveUnit').val("%");
                    $('#selectHaveMode').val("房款总额");
                } else if (tyoumode == 3) {
                    $('#selectHaveUnit').val("元");
                    $('#selectHaveMode').val("1");
                }
            }
            if (twumode != 1) {
                twumode = twumode.substring(0, twumode.length - 1);
                if (twumode == 1) {
                    $('#selectNoUnit').val("元");
                    $('#selectNoMode').val("面积");
                } else if (twumode == 2) {
                    $('#selectNoUnit').val("%");
                    $('#selectNoMode').val("房款总额");
                } else if (twumode == 3) {
                    $('#selectNoUnit').val("元");
                    $('#selectNoMode').val("1");
                }
            }
            $('#minArea').val(data.r90areato);
            $('#minTax').val(data.r90);
            $('#minArea2').val(data.rx144areafrom);
            $('#maxArea2').val(data.rx144areato);
            $('#normalTax2').val(data.rx144);
            $('#maxArea3').val(data.rd144areafrom);
            $('#maxTax3').val(data.rd144);
            $('#tax4').val(data.rt2);
            $('#tax5').val(data.rfs);
            $('#txtStampTax').val(data.yinhua);
            $('#txtHaveElevatorNum').val(data.tyou);
            $('#txtNoElevatorNum').val(data.twu);
            $('#txtRegistration').val(data.qs);
        },

        save: function () {
            var data = {};
            data.stampduty = 0;
            data.maintenancefund = [];
            data.regist = 0;
            var deed = [];
            //存储契税规则
            var item = {
               is_sp_build: '1',
               is_first_room: '1',
               area_from: '0',
               area_to: $('#minArea').val(),
               rate: $('#minTax').val()
            };
            deed.push(item);
            item = {
               is_sp_build: '1',
               is_first_room: '1',
               area_from: $('#minArea2').val(),
               area_to: $('#maxArea2').val(),
               rate: $('#normalTax2').val()
            };
            deed.push(item);
            item = {
               is_sp_build: '1',
               is_first_room: '1',
               area_from: $('#maxArea3').val(),
               area_to: '99999999',
               rate: $('#maxTax3').val()
            };
            deed.push(item);
            item = {
               is_sp_build: '1',
               is_first_room: '0',
               area_from: '0',
               area_to: '0',
               rate: $('#tax4').val()
            };
            deed.push(item);
            item = {
               is_sp_build: '0',
               is_first_room: '0',
               area_from: '0',
               area_to: '0',
               rate: $('#tax5').val()
            };
            deed.push(item);
            data.deed = JSON.stringify(deed);
            //存储印花税规则
            data.stampduty = $("#txtStampTax").val();
            //存储维修基金规则
            var have_cal_type = 0; //有电梯计算模式
            var have_amout = 0; //有电梯固定金额
            var have_rate = 0; //有电梯的费率
            var no_cal_type = 0; //无电梯计算模式
            var no_amout = 0; //无电梯固定金额
            var no_rate = 0; //无电梯的费率

            if ($('#selectHaveMode').val() == "房款总额") {
               have_cal_type = 2;
               have_amout = 0;
               have_rate = $('#txtHaveElevatorNum').val();
            }
            else if ($('#selectHaveMode').val() == "面积") {
               have_cal_type = 1;
               have_amout = $('#txtHaveElevatorNum').val();
               have_rate = 0;
            }
            else if ($('#selectHaveMode').val() == "1") {
               have_cal_type = 3;
               have_amout = $('#txtHaveElevatorNum').val();
               have_rate = 0;
            }

            if ($('#selectNoMode').val() == "房款总额") {
               no_cal_type = 2;
               no_amout = 0;
               no_rate = $('#txtNoElevatorNum').val();
            }
            else if ($('#selectNoMode').val() == "面积") {
               no_cal_type = 1;
               no_amout = $('#txtNoElevatorNum').val();
               no_rate = 0;
            }
            else if ($('#selectNoMode').val() == "1") {
               no_cal_type = 3;
               no_amout = $('#txtNoElevatorNum').val();
               no_rate = 0;
            }


            var maintenancefund = [];
            item = {
               is_have_elevator: '1',
               cal_type: have_cal_type,
               fixed_amount: have_amout,
               rate: have_rate
            };
            maintenancefund.push(item);
            item = {
               is_have_elevator: '0',
               cal_type: no_cal_type,
               fixed_amount: no_amout,
               rate: no_rate
            };
            maintenancefund.push(item);
            data.maintenancefund = JSON.stringify(maintenancefund);
            //存储权属登记费规则
            data.regist = $("#txtRegistration").val();

            var jsonStr = JSON.stringify(data);
            var postData = { data: jsonStr };

            if(this.isSubmit){
                ui.showErrTip("请勿重复保存");
                return;
            }
            ui.loading(null,"正在保存...");
            var url = api_prefix + "write-tax-setting&token=" +token;
            util.myAjax({
                url: url,
                type: "POST",
                cache: false,
                context: this,
                data: data,
                success: function (result) {
                    ui.removeLoading();
                    if (result.retCode == "0") {
                       ui.showSucTip("保存成功");
                    } else {
                       ui.showErrTip("保存失败");
                    }
                },
                complete:function(){
                    this.isSubmit = false;
                }
            });
            this.isSubmit = true;
        },

        equalNumber: function (id,id2) {
            $("#"+id).on("keyup",function(){
                var _this = this;
                var val = $(_this).val();
                $("#"+id2).val(val);
                $(_this).on("blur",function(){
                    var val = $(_this).val();
                    $("#"+id2).val(val);
                });
            });
        },

        // 初始化事件
        initEvents: function () {
            this.checkNumber();
            this.initSelect();
            this.getValue();
            this.equalNumber("minArea","minArea2");
            this.equalNumber("minArea2","minArea");
            this.equalNumber("maxArea2","maxArea3");
            this.equalNumber("maxArea3","maxArea2");

            $(".js-change a").click(function(){
                var text = $(this).text();
                var input = $(this).parent().prev().find("input");
                input.val(text);
                $(this).parent().parent().removeClass("select-on");
                var next_input = $(this).parent().parent().next().next().find("input");
                if(text =="元"){
                    next_input.val("面积");
                }else{
                    next_input.val("房款总额");
                }
            });
            $(".js-change-sub a").click(function(){
                var text = $(this).text();
                var input = $(this).parent().prev().find("input");
                input.val(text);
                $(this).parent().parent().removeClass("select-on");
                var prev_input = $(this).parent().parent().prev().prev().find("input");
                if(text =="房款总额"){
                    prev_input.val("%");
                }else{
                    prev_input.val("元");
                }
            });

            // 保存
            $("#save").click(function(){
                if(service.checkForm()){
                    service.save();
                }
            });
        },

        // 初始化
        init: function () {
            this.initEvents();
        }

    };
    service.init();
    module.exports = service;
});
